package com.gxp.util.common;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;

public class GXPUtil {

	public static String getJsonString (List<String> messageList, boolean accessDenied, String cause) {
		String jsonObject = "";
		if (GXPUtil.isEmpty(messageList)) {
			jsonObject = "{\"message\":\"" + "Unknow Error" + "\","
					+ "\"accessDenied\":" + accessDenied + ",\"cause\":\"" + cause + "\"}";
		} else {
			
			StringBuffer sb = new StringBuffer();
			for (String message : messageList) {
				sb.append(message).append("\\n");
			}
			jsonObject = "{\"message\":\"" + sb.toString() + "\","
					+ "\"accessDenied\":" + accessDenied + ",\"cause\":\"" + cause + "\"}";
		}
		return jsonObject;
	}
	
	public static String getJsonString (String message, boolean accessDenied, String cause) {
		String jsonObject = "";
		if (GXPUtil.isEmpty(message)) {
			jsonObject = "{\"message\":\"" + "Unknow Error" + "\","
					+ "\"accessDenied\":" + accessDenied + ",\"cause\":\"" + cause + "\"}";
		} else {			
			
			jsonObject = "{\"message\":\"" + message + "\","
					+ "\"accessDenied\":" + accessDenied + ",\"cause\":\"" + cause + "\"}";
		}
		
		System.out.println(jsonObject);
		return jsonObject;
	}
	
	public static String getJsonString (String key, String value) {
		return "{\"" + key + "\":\"" + value + "\"}";
	}
	
	public static String getJsonString (Map<String, String> keyvalueMap) {
		StringBuffer bf = new StringBuffer();
		bf.append("{");
		int i = 0;
		for (Iterator<String> iter = keyvalueMap.keySet().iterator(); iter.hasNext(); ) {
			String key = iter.next();
			String value = keyvalueMap.get(key);
			if (i != 0) {
				bf.append(",");
			}
			bf.append("\"" + key + "\":\"" + value + "\"");
			i++;
		}
		bf.append("}");
		return bf.toString();
	}
	
	/**
     * this method is used to check whether field(object) is empty or null
     * 
     * @param object
     *            is String, Integer, List and so on
     * @return return true if is null or empty; otherwise return false
     */
    public static boolean isEmpty(Object object) {
        if (isNull(object)) {
            return true;
        } else {
            int length = -1;
            if (object instanceof String) {
                String field = (String) object;
                length = field.trim().length();
            } else if (object instanceof String[]) {
                String[] fields = (String[]) object;
                length = fields.length;
            } else if (object instanceof List) {
                List fieldL = (List) object;
                length = fieldL.size();
            } else if (object instanceof ArrayList) {
                ArrayList fieldAL = (ArrayList) object;
                length = fieldAL.size();
            } else if (object instanceof LinkedList) {
                LinkedList fieldLL = (LinkedList) object;
                length = fieldLL.size();
            } else if (object instanceof Map) {
                Map fieldM = (Map) object;
                length = fieldM.size();
            } else if (object instanceof HashMap) {
                HashMap fieldHM = (HashMap) object;
                length = fieldHM.size();
            } else if (object instanceof Set) {
                Set fieldS = (Set) object;
                length = fieldS.size();
            }
            return length == 0;
        }
    }
    
    /**
     * this method is used to check whether field(object) is null
     * 
     * @param object
     *            is String, Integer, List and so on
     * @return return true if is null; otherwise return false
     */
    public static boolean isNull(Object object) {
        return object == null;
    }
    
    /**
     * this method is used to format list for some param
     * 
     * @param param
     *            is parameter use , split
     * @return list contain string
     */
    public static List<String> formatStringToList(String param) {
        List<String> list = new ArrayList<String>();
        if (!isEmpty(param)) {

            StringTokenizer st = new StringTokenizer(param, ",");

            while (st.hasMoreTokens()) {
                String str = st.nextToken().trim();
                if (!list.contains(str)) {
                    list.add(str);
                }
            }

        }
        return list;
    }

    /**
     * this method is used to format list for some param
     * 
     * @param param
     *            is parameter
     * @param length
     *            result list
     * @return is array contain string
     */
    public static String[] formatStringToArray(String param, int length) {
        List<String> list = formatStringToList(param);
        String[] arrays = null;
        int size = 0;
        if (!isEmpty(list)) {
            size = list.size();
            arrays = new String[size];
            Iterator<String> iterator = list.iterator();
            int count = 0;
            while (iterator.hasNext()) {
                arrays[count] = (String) iterator.next();
                count++;
            }
        }

        if (size == 0) {
            arrays = fillArrayLength(arrays, length);
        }
        return arrays;
    }
    
    /**
     * this method is used to fill array to match length
     * 
     * @param values
     *            is input value array
     * @param length
     *            is fill length
     * @return array contain values and fill length use ""
     */
    public static String[] fillArrayLength(String[] values, int length) {
        return fillArrayLength(values, length, "");
    }

    /**
     * this method is used to fill array to match length
     * 
     * @param values
     *            is input value array
     * @param length
     *            is fill length
     * @param fillValue
     *            fill value
     * @return array contain values and fill length use ""
     */
    public static String[] fillArrayLength(String[] values, int length, String fillValue) {
        String[] fillValues = new String[length];
        int i;
        if (isEmpty(values)) {
            i = 0;
        } else {
            i = values.length;
            if (i > length) {
                i = length;
            }
            String value;
            for (int j = 0; j < i; j++) {
                value = values[j];
                if (isNull(value)) {
                    value = fillValue;
                }
                fillValues[j] = value;
            }
        }
        for (; i < length; i++) {
            fillValues[i] = fillValue;
        }
        return fillValues;
    }
}
